//
//  KKAreaPicker.m
//  PhnixHome
//
//  Created by Levy Xu on 2018/1/8.
//  Copyright © 2018年 Levy Xu. All rights reserved.
//

#import "KKAreaPicker.h"

typedef enum : NSUInteger {
    BLComponentTypeProvince = 0, // 省
    BLComponentTypeCity,         // 市
    BLComponentTypeArea,         // 区
} BLComponentType;

@interface KKAreaPicker () <UIPickerViewDelegate, UIPickerViewDataSource>
@property(nonatomic, strong) NSArray *areaArray;
@property(nonatomic, strong) UIPickerView *pickView;
@end

@implementation KKAreaPicker {
    NSInteger _provinceSelectedRow;
    NSInteger _citySelectedRow;
    NSInteger _areaSelectedRow;
}

- (void)didInitialized {
    [super didInitialized];
    
    NSString *plistStr = [[NSBundle mainBundle] pathForResource:@"areaArray" ofType:@"plist"];
    _areaArray = [[NSArray alloc] initWithContentsOfFile:plistStr];
    
    BeginIgnoreAvailabilityWarning
    [self loadViewIfNeeded];
    EndIgnoreAvailabilityWarning
}

- (void)initSubviews {
    [super initSubviews];
    
    self.pickView = [[UIPickerView alloc] init];
    self.pickView.dataSource = self;
    self.pickView.delegate = self;
    [self.view addSubview:self.pickView];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.pickView.frame = CGRectMake(0, CGRectGetMaxY(self.headerView.frame), CGRectGetWidth(self.view.bounds), (!self.footerView.hidden ? CGRectGetMinY(self.footerView.frame) : CGRectGetHeight(self.view.bounds)) - CGRectGetMaxY(self.headerView.frame));
}

-(void)setProvince:(NSString *)province city:(NSString *)city area:(NSString *)area {
    _provinceSelectedRow = 0;
    _citySelectedRow = 0;
    _areaSelectedRow = 0;
    
    for (int i = 0; i < _areaArray.count; i++) {
        NSDictionary *provinceDic = _areaArray[i];
        if ([provinceDic[@"provinceName"] isEqualToString:province]) {
            _provinceSelectedRow = i;
            
            NSArray *cityArr = [provinceDic objectForKey:@"citylist"];
            for (int j = 0; j < cityArr.count; j++) {
                NSDictionary *cityDic = cityArr[j];
                if ([cityDic[@"cityName"] isEqualToString:city]) {
                    _citySelectedRow = j;
                    NSArray *areaArr = [cityDic objectForKey:@"arealist"];
                    for (int z = 0; z < areaArr.count; z++) {
                        NSDictionary *areaDic = areaArr[z];
                        if ([areaDic[@"areaName"] isEqualToString:area]) {
                            _areaSelectedRow = z;
                            break;
                        }
                    }
                    break;
                }
            }
            break;
        }
    }
    NSLog(@"_provinceSelectedRow %ld; _citySelectedRow %ld; _areaSelectedRow %ld",_provinceSelectedRow, _citySelectedRow,_areaSelectedRow);
    _selectedProvinceTitle = [self pickerView:_pickView titleForRow:_provinceSelectedRow forComponent:0];
    _selectedCityTitle = [self pickerView:_pickView titleForRow:_citySelectedRow forComponent:1];
    _selectedAreaTitle = [self pickerView:_pickView titleForRow:_areaSelectedRow forComponent:2];
    NSLog(@"last %@ %@ %@", _selectedProvinceTitle, _selectedCityTitle, _selectedAreaTitle);
    
    [_pickView selectRow:_provinceSelectedRow inComponent:0 animated:YES];
    [_pickView selectRow:_citySelectedRow inComponent:1 animated:YES];
    [_pickView selectRow:_areaSelectedRow inComponent:2 animated:YES];
}

#pragma mark - - UIPickerViewDelegate,UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    switch (component) {
        case BLComponentTypeProvince:
            return _areaArray.count;
            break;
        case BLComponentTypeCity:
            return [[[_areaArray objectAtIndex:_provinceSelectedRow] objectForKey:@"citylist" ] count];
            break;
        case BLComponentTypeArea:
            return [[[[[_areaArray objectAtIndex:_provinceSelectedRow] objectForKey:@"citylist" ] objectAtIndex:_citySelectedRow] objectForKey:@"arealist" ] count];
            break;
        default:
            return _areaArray.count;
            break;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    NSDictionary *provinceDic = [_areaArray objectAtIndex:_provinceSelectedRow];
    NSArray *cityArr = [provinceDic objectForKey:@"citylist"];
    
    switch (component) {
        case BLComponentTypeProvince:
            return [_areaArray[row] objectForKey:@"provinceName"];
            break;
        case BLComponentTypeCity:{
            NSDictionary *cityDic = [cityArr objectAtIndex:row];
            return [cityDic objectForKey:@"cityName"];
            break;
        }
        case BLComponentTypeArea:{
            NSDictionary *areaDic = [cityArr objectAtIndex:_citySelectedRow];
            NSArray *areaArr = [areaDic objectForKey:@"arealist"];
            return [areaArr[row] objectForKey:@"areaName"];
            break;
        }
        default:
            return [_areaArray[row] objectForKey:@"provinceName"];
            break;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    switch (component) {
        case BLComponentTypeProvince:{
            _provinceSelectedRow = row;
            _citySelectedRow = 0;
            _areaSelectedRow = 0;
            [pickerView selectRow:0 inComponent:1 animated:NO];
            [pickerView selectRow:0 inComponent:2 animated:NO];
            break;
        }
        case BLComponentTypeCity:{
            _citySelectedRow = row;
            _areaSelectedRow = 0;
            [pickerView selectRow:0 inComponent:2 animated:NO];
            break;
        }
        case BLComponentTypeArea:
            _areaSelectedRow = row;
            break;
        default:
            _provinceSelectedRow = row;
            break;
    }
    
    _selectedProvinceTitle = [self pickerView:_pickView titleForRow:_provinceSelectedRow forComponent:0];
    _selectedCityTitle = [self pickerView:_pickView titleForRow:_citySelectedRow forComponent:1];
    _selectedAreaTitle = [self pickerView:_pickView titleForRow:_areaSelectedRow forComponent:2];
    
    [pickerView reloadAllComponents];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return self.view.qmui_width / 3;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:SourceFontMake(14)];
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

#pragma mark - <QMUIModalPresentationContentViewControllerProtocol>
- (CGSize)preferredContentSizeInModalPresentationViewController:(QMUIModalPresentationViewController *)controller limitSize:(CGSize)limitSize {
    CGFloat pickerHeight = 216;
    return CGSizeMake(limitSize.width, CGRectGetHeight(self.headerView.frame) + pickerHeight + (!self.footerView.hidden ?  CGRectGetHeight(self.footerView.frame) : 0));
}

@end
